package qm.com.servlet;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import qm.com.dto.QingMingActive;
import qm.com.service.QingmingService;

/**
 * 
 * Spring 简单的增删改查的Demo 测试类 完成功能
 * 
 * 每发送过来一个请求 就会创建一个SpringDemoServletTest实例 每次的times都不一样
 * 
 * */
@Controller
public class SpringDemoServletTest {
	Logger logger = Logger.getLogger(SpringDemoServletTest.class);
    /**
     * 返回的数据接收times；数据库操作的次数
     * */
	private int times;

	@Resource
	private QingmingService qingmingService;

	@RequestMapping(value = "/insertQingming")
	public ResponseEntity<String> insertQingming(QingMingActive qingMingActive,
			HttpServletRequest request) {
		qingMingActive.getEat();
		qingMingActive.getPlay();

		try {
			times = qingmingService.insertQMAction(qingMingActive);
		} catch (Exception e) {
			logger.error("SQLExcepton", e);
			e.printStackTrace();
		}
		if (times == 1) {
			return new ResponseEntity<String>("Insert succeed.", HttpStatus.OK);
		}
		logger.error("Can not find the information");
		return new ResponseEntity<String>("Insert failed,please try again.",
				HttpStatus.OK);
	}

	@RequestMapping(value = "/selectQingming")
	public ResponseEntity<String> selectQingming(String eat,
			HttpServletRequest request) {
		List<QingMingActive> list = null;
		try {
			list = qingmingService.selectQMAction(eat);
		} catch (Exception e) {
			logger.error("SQLException", e);
			e.printStackTrace();
		}
		logger.info("List::" + list.toString());
		if (!list.isEmpty()) {
			return new ResponseEntity<String>(list.get(0).getPlay(),
					HttpStatus.OK);// 添加http状态码
		}
		logger.error("Can not find the information,eat:" + eat
				+ " was not find.");
		return new ResponseEntity<String>("Can not find the information.",
				HttpStatus.OK);
	}

	@RequestMapping(value = "/updateQingming")
	public ResponseEntity<String> updateQingming(QingMingActive qingMingActive,
			HttpServletRequest request) {
		try {
			times = qingmingService.updateQMAction(qingMingActive);
		} catch (Exception e) {
			logger.error("SQLException", e);
			e.printStackTrace();
		}
		if (times >= 1) {
			return new ResponseEntity<String>("Update succeed.", HttpStatus.OK);
		}

		logger.error("Can not find the information, eat:"
				+ qingMingActive.getEat() + " was not find.");

		return new ResponseEntity<String>("Can not find the eat information.",
				HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteQingming")
	public ResponseEntity<String> deleteQingming(String eat,
			HttpServletRequest request) {
		try {
			times = qingmingService.deleteQMAction(eat);
		} catch (Exception e) {
			logger.error("SQLException", e);
			e.printStackTrace();
		}
		if (times >= 1) {
			return new ResponseEntity<String>("delete succeed.", HttpStatus.OK);
		}
		logger.error("Can not find the information, eat:" + eat
				+ " was not find.");
		return new ResponseEntity<String>("Can not find the eat information.",
				HttpStatus.OK);
	}

	
	@RequestMapping(value = "/selectlistQingming")
	public ResponseEntity<String> selectlistQingming(String eat,
			HttpServletRequest request) {
		List<QingMingActive> list = null;
		try {
			list = qingmingService.selectQMAction(eat);
		} catch (Exception e) {
			logger.error("SQLException", e);
			e.printStackTrace();
		}
		/**
		 * 
		 * 把一个List类型的数据转成Json类型 传到网页  网页再解析
		 * 
		 * 网页无法读取List类型的数据
		 * 
		 * */
		JSONArray jsonArray = JSONArray.fromObject(list);
		logger.info("List::" + list.toString());

		if (!list.isEmpty()) {
			// 添加http状态码
			return new ResponseEntity<String>(jsonArray.toString(),
					HttpStatus.OK);
		}
		logger.error("Can not find the information,eat:" + eat
				+ " was not find.");
		return new ResponseEntity<String>("Can not find the information,eat:"
				+ eat + " was not find.", HttpStatus.OK);
	}
}
